package fr.mma.livraison;
import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import fr.mma.livraison.controler.AdminDao;
import fr.mma.livraison.model.Admin;

@SuppressWarnings("serial")
public class AdminServlet extends HttpServlet {
	private AdminDao dao = new AdminDao();

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Gson gson = new Gson();
		if (dao.getAll().isEmpty()){
			Admin admin = new Admin();
			admin.setId(1);
			admin.setName("mkl.lecoq@gmail.com");
			dao.save(admin);
		}
		if(req.getUserPrincipal() != null && dao.isAdmin(req.getUserPrincipal().getName())) {
			resp.getWriter().println(gson.toJson("ok"));
		} else {
			resp.getWriter().println(gson.toJson("ko"));
		}
	}
}
